import requests
from bs4 import BeautifulSoup
import pymysql

db = pymysql.connect("localhost", "root", "root", "upi")  # 打开数据库连接
cursor = db.cursor()  # 获取游标对象
headers = {
    'authority': 'www.chunyuyisheng.com',
    'upgrade-insecure-requests': '1',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36',
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
    'sec-fetch-site': 'same-origin',
    'sec-fetch-mode': 'navigate',
    'sec-fetch-user': '?1',
    'sec-fetch-dest': 'document',
    'referer': 'https://www.chunyuyisheng.com/pc/doctors/0-0-17/?page=2',
    'accept-language': 'zh-CN,zh;q=0.9',
    'cookie': 'Hm_lvt_c153f37e6f66b16b2d34688c92698e4b=1618883924; Hm_lpvt_c153f37e6f66b16b2d34688c92698e4b=1618884077',
}

params = (
    ('page', '1'),
)

r = requests.get('https://www.chunyuyisheng.com/pc/doctors/0-0-17/', headers=headers, params=params)
html = BeautifulSoup(r.text, 'lxml')
index = 1
for i in html.select('body > div.ui-grid.ui-main.clearfix > div.doctor-list > div.doctor-info-item'):
    data = {
        "name": i.select_one('div > div.detail > div:nth-child(1) > a > span.name').get_text()[:-1],
        "cover": i.select_one('div > div.avatar-wrap > a > img')['src'],
        "office": i.select_one('div > div.detail > div:nth-child(1) > a > span.clinic').get_text()[:-1],
        "level": i.select_one('div > div.detail > div:nth-child(1) > a > span.grade').get_text(),
        "hospital": i.select_one('div > div.detail > div:nth-child(2) > a').get_text()[5:-4],
        "serve": int(i.select_one('div > div.detail > div:nth-child(3) > span:nth-child(1) > i').get_text()),
        "score": round(
            float(i.select_one('div > div.detail > div:nth-child(3) > span:nth-child(2) > i').get_text()) / 20),
        "intro": i.select_one('div > div.detail > p').get_text()
    }
    print(data)
    cursor.execute(
        f"""INSERT INTO doctor VALUES({index},'{data['name']}','{data['cover']}','{data['office']}','{data['level']}','{data['hospital']}',{data['serve']},{data['score']},'{data['intro']}','[]')""")
    db.commit()
    index += 1
